package com.daguchuangyi.erp_android.productlist.productsummary.widge;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.daguchuangyi.erp_android.ErpApplication;
import com.daguchuangyi.erp_android.R;
import com.daguchuangyi.erp_android.model.LocalMediaWrapper;
import com.daguchuangyi.erp_android.productlist.classifiaction.ClassificationManager;
import com.daguchuangyi.erp_android.util.ImageUtil;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lijianjian on 2018/3/12.
 */

public class ImageTellHeader extends ConstraintLayout {

    private RecyclerView mTellResultRv;
    private TellResultAdapter mAdapter;

    public ImageTellHeader(Context context) {
        super(context);
        init(context);
    }

    public ImageTellHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ImageTellHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.view_image_tell_result_layout, this);

        findViewById(R.id.clear_tv).setOnClickListener(view -> {
            setVisibility(GONE);
            ClassificationManager.getInstance().quitRecognitionMode();
        });

        mTellResultRv = findViewById(R.id.tell_result_rv);
        mTellResultRv.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        mAdapter = new TellResultAdapter(null);
        mTellResultRv.setAdapter(mAdapter);
        DividerItemDecoration decoration = new DividerItemDecoration(context, DividerItemDecoration.HORIZONTAL);
        decoration.setDrawable(ContextCompat.getDrawable(context, R.drawable.divider_gallery_pendding));
        mTellResultRv.addItemDecoration(decoration);
    }

    public void bindData(LocalMediaWrapper localMediaWrapper) {
        List<LocalMediaWrapper> localMediaList = new ArrayList<>();
        localMediaList.add(localMediaWrapper);
        mAdapter.setNewData(localMediaList);
    }

    private class TellResultAdapter extends BaseQuickAdapter<LocalMediaWrapper, BaseViewHolder> {

        public TellResultAdapter(@Nullable List<LocalMediaWrapper> data) {
            super(R.layout.item_tell_result_layout, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, LocalMediaWrapper localMediaWrapper) {
            String path = localMediaWrapper.getLocalMedia().getPath();
            if (!TextUtils.isEmpty(localMediaWrapper.getLocalMedia().getCutPath())) {
                path = localMediaWrapper.getLocalMedia().getCutPath();
            }
            Glide.with(ErpApplication.getAppContext())
                    .applyDefaultRequestOptions(ImageUtil.getProductSummaryOptions())
                    .load(path)
                    .into((ImageView) helper.getView(R.id.item_result_iv));

            helper.getView(R.id.close_iv).setOnClickListener(view -> {
                getData().remove(localMediaWrapper);
                notifyItemRemoved(helper.getAdapterPosition());
            });
        }
    }

}
